﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CRM.Component.Business;
using TRPOP.CRM.Component.Common;

namespace TRPOP.CRM.Web
{
    public partial class Company_AllList : BaseAdmin
    {
        #region 权限限制
        protected override void SubPageInit()
        {
            base.m_oCurrentPermissionKey = "Company_AllList";
        }
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
                BindData();
            }
        }

        /// <summary>
        /// 分页触发此事件（分页采用post方法传递数据）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void UC_PagerChanged(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>        
        /// 删除管理员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;

            if (CompanyManage.DeleteInfoById(lbtn.CommandArgument.ToInt()))
            {
                Alert("删除成功！");
            }
            else
            {
                Alert("删除失败，要删除的客户已被占用或正在跟进中");
            }

            Pager.Page_ReLoad();
            BindData();
        }

        /// <summary>
        /// 批量删除管理员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strRegiterIds = RequestHelper.RequestString("delid");

            if (strRegiterIds.IsEmpty())
            {
                return;
            }

            if (CompanyManage.DeleteInfoByIds(strRegiterIds))
            {
                Alert("所选信息的已删除");
            }
            else
            {
                Alert("删除失败，要删除的客户已被占用或正在跟进中");
            }

            Pager.Page_ReLoad();
            BindData();
        }
        #endregion

        #region 页面方法
        void LoadBase()
        {
            PageBindHelper.BindCompanyStatus(ddlCompanyStatus);
            ddlCompanyStatus.Items.Insert(0, new ListItem("全部", "-1"));
        }
        /// <summary>
        /// 绑定管理员信息列表
        /// </summary>
        protected void BindData()
        {
            string strCondation = string.Empty;

            if (!txtName.Text.IsEmpty() && txtName.Text != "输入关键字")
            {
                strCondation = string.Format(" AND ( nvcCompanyName LIKE '%{0}%'  OR nvcRegisterAdress LIKE '%{0}%'  OR nvcContactPhone LIKE '%{0}%' OR nvcContactName LIKE '%{0}%'  OR nvcEmployeeName  LIKE '%{0}%')  ", txtName.Text.Trim().HtmlEncode().SQLFilter());
            }

            if (ddlCompanyStatus.SelectedValue != "-1")
            {
                strCondation += string.Format(" AND iCompanyStatus={0} ", ddlCompanyStatus.SelectedValue);
            }

            int RecordCount = 0;

            rptList.DataSource = CompanyManage.GetExtraInfoListByPager(Pager.CurrentPage, Pager.PageSize, strCondation, string.Empty, out RecordCount);

            rptList.DataBind();

            Pager.RecordCount = RecordCount;
        }
        #endregion
    }
}